package com.whnk.attendance.service.impl;

import java.util.Date;
import java.util.List;

import com.whnk.attendance.domain.AreaInfo;
import com.whnk.attendance.mapper.AreaInfoMapper;
import com.whnk.attendance.service.IAreaInfoService;
import com.whnk.attendance.vo.AreaInfoListVo;
import com.whnk.attendance.vo.AreaListVo;
import com.whnk.attendance.vo.AreaInfoQueryVo;
import com.whnk.attendance.vo.AreaInfoUpdateVo;
import com.whnk.common.core.exception.ServiceException;
import com.whnk.common.core.utils.DateUtils;
import com.whnk.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 区域分类信息Service业务层处理
 *
 * @author whnk
 * @date 2021-12-10
 */
@Service
public class AreaInfoServiceImpl implements IAreaInfoService
{

    @Autowired
    private AreaInfoMapper areaInfoMapper;

    /**
     * 查询区域分类信息
     *
     * @param areaId 区域分类信息主键
     * @return 区域分类信息
     */
    @Override
    public AreaInfo selectAreaInfoByAreaId(String areaId)
    {
        return areaInfoMapper.selectAreaInfoByAreaId(areaId);
    }

    /**
     * 查询区域分类信息列表
     *
     * @param areaInfo 区域分类信息
     * @return 区域分类信息
     */
    @Override
    public List<AreaInfo> selectAreaInfoList(AreaInfo areaInfo)
    {
        return areaInfoMapper.selectAreaInfoList(areaInfo);
    }

    /**
     * 新增区域分类信息
     *
     * @param areaInfo 区域分类信息
     * @return 结果
     */
    @Override
    public int insertAreaInfo(AreaInfo areaInfo)
    {
        areaInfo.setCreateTime(DateUtils.getNowDate());
        return areaInfoMapper.insertAreaInfo(areaInfo);
    }

    /**
     * 修改区域分类信息
     *
     * @param areaInfo 区域分类信息
     * @return 结果
     */
    @Override
    public int updateAreaInfo(AreaInfo areaInfo)
    {
        areaInfo.setUpdateTime(DateUtils.getNowDate());
        return areaInfoMapper.updateAreaInfo(areaInfo);
    }

    /**
     * 批量删除区域分类信息
     *
     * @param areaIds 需要删除的区域分类信息主键
     * @return 结果
     */
    @Override
    public int deleteAreaInfoByAreaIds(String[] areaIds)
    {
        return areaInfoMapper.deleteAreaInfoByAreaIds(areaIds);
    }

    /**
     * 删除区域分类信息信息
     *
     * @param areaId 区域分类信息主键
     * @return 结果
     */
    @Override
    public int deleteAreaInfoByAreaId(String areaId)
    {
        return areaInfoMapper.deleteAreaInfoByAreaId(areaId);
    }

    /**
     * 根据区域交付编号查询区域信息
     */
    public AreaInfoListVo selectAreaInfoListVoByAreaId(String areaId) {
        return areaInfoMapper.selectAreaInfoListVoByAreaId(areaId);
    }

    /**
     * 分页查询区域列表
     */
    public List<AreaInfoListVo> selectAreaInfoListVoList(AreaInfoQueryVo areaInfoQueryVo) {
        return areaInfoMapper.selectAreaInfoListVoList(areaInfoQueryVo);
    }

    /**
     * 校验交付区域编号是否唯一
     */
    public boolean checkAreaId(AreaInfoUpdateVo areaInfoUpdateVo){
        int count = areaInfoMapper.checkAreaId(areaInfoUpdateVo.getAreaId());
        if(count > 0){
            return false;
        }
        return true;
    }

    /**
     * 新增区域信息
     */
    public int insertAreaInfo(AreaInfoUpdateVo areaInfoUpdateVo) {
        AreaInfo areaInfo = new AreaInfo();
        Date date = new Date();
        areaInfo.setAreaId(areaInfoUpdateVo.getAreaId());
        areaInfo.setAreaName(areaInfoUpdateVo.getAreaName());
        areaInfo.setAreaManager(areaInfoUpdateVo.getAreaManager());
        areaInfo.setParentId(areaInfoUpdateVo.getParentId());
        areaInfo.setAreaLevel(areaInfoUpdateVo.getAreaLevel());
        areaInfo.setCreateTime(date);
        return areaInfoMapper.insertAreaInfo(areaInfo);
    }

    /**
     * 修改区域信息
     */
    public int updateAreaInfo(AreaInfoUpdateVo areaInfoUpdateVo) {
        AreaInfo areaInfo = new AreaInfo();
        Date date = new Date();
        areaInfo.setAreaId(areaInfoUpdateVo.getAreaId());
        areaInfo.setAreaName(areaInfoUpdateVo.getAreaName());
        areaInfo.setAreaManager(areaInfoUpdateVo.getAreaManager());
        areaInfo.setParentId(areaInfoUpdateVo.getParentId());
        areaInfo.setAreaLevel(areaInfoUpdateVo.getAreaLevel());
        areaInfo.setUpdateTime(date);
        return areaInfoMapper.updateAreaInfo(areaInfo);
    }

    /*
     * 导入区域信息
     */
    public String importData(List<AreaInfoListVo> areaInfoList, Boolean isUpdateSupport){
        if (StringUtils.isNull(areaInfoList) || areaInfoList.size() == 0)
        {
            throw new ServiceException("导入区域信息不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (AreaInfoListVo areaInfoListVo : areaInfoList)
        {
            try
            {
                // 验证是否存在这个区域信息
                AreaInfoListVo area = areaInfoMapper.selectAreaInfoListVoByAreaId(areaInfoListVo.getAreaId());
                if (StringUtils.isNull(area))
                {
                    AreaInfoUpdateVo areaInfoUpdateVo = new AreaInfoUpdateVo();
                    areaInfoUpdateVo.setAreaId(areaInfoListVo.getAreaId());
                    areaInfoUpdateVo.setAreaName(areaInfoListVo.getAreaName());
                    areaInfoUpdateVo.setAreaManager(areaInfoListVo.getAreaManager());
                    areaInfoUpdateVo.setParentId(areaInfoListVo.getParentId());
                    areaInfoUpdateVo.setAreaLevel(areaInfoListVo.getAreaLevel());
                    this.insertAreaInfo(areaInfoUpdateVo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、区域 " + areaInfoListVo.getAreaName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    AreaInfoUpdateVo areaInfoUpdateVo = new AreaInfoUpdateVo();
                    areaInfoUpdateVo.setAreaId(areaInfoListVo.getAreaId());
                    areaInfoUpdateVo.setAreaName(areaInfoListVo.getAreaName());
                    areaInfoUpdateVo.setAreaManager(areaInfoListVo.getAreaManager());
                    areaInfoUpdateVo.setParentId(areaInfoListVo.getParentId());
                    areaInfoUpdateVo.setAreaLevel(areaInfoListVo.getAreaLevel());
                    this.updateAreaInfo(areaInfoUpdateVo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、区域 " + areaInfoListVo.getAreaName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、区域 " + areaInfoListVo.getAreaName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、区域 " + areaInfoListVo.getAreaName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /*
     * 交付区域编号id，交付区域名称name接口
     */
    public List<AreaListVo> areaList(){
        return areaInfoMapper.areaList();
    }
}